package com.yuyou.fn.analyze.service.impl;

import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.Principals;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.entity.StudentRefund;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.service.IStudentRefundService;
import com.yuyou.fn.report.entity.MergeStudentRegDetail;
import com.yuyou.fn.report.service.IMergeStudentRegDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.yuyou.fn.analyze.mapper.RefundBonusPersonMapper;
import com.yuyou.fn.analyze.entity.RefundBonusPerson;
import com.yuyou.fn.analyze.service.IRefundBonusPersonService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import javax.annotation.Resource;
import java.sql.Time;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * RefundBonusPerson 表数据服务层接口实现类
 *
 */
@Service
public class RefundBonusPersonServiceImpl extends SuperServiceImpl<RefundBonusPersonMapper, RefundBonusPerson,Long> implements IRefundBonusPersonService {

    @Resource
    private IStudentRefundService studentRefundService;

    @Resource
    private IMergeStudentRegDetailService mergeStudentRegDetailService;

    @Resource
    private ICourseClassService courseClassService;

    @Override
    public void saveRefundBonusPerson(List<Long> studentRefundIds) {
        if (Utils.isEmpty(studentRefundIds)){
            return;
        }
        Principals.Principal principal=Principals.get();
        List<RefundBonusPerson> refundBonusPersonList=this.findByIds(studentRefundIds);
        List<Long> removeIds = BeanUtils.toList(refundBonusPersonList, "studentRefundId");
        studentRefundIds.removeAll(removeIds);
        if (!Utils.isEmpty(studentRefundIds)){
            List<StudentRefund> studentRefundList=studentRefundService.findByIds(studentRefundIds);
            List<RefundBonusPerson> insertList=new ArrayList<>(studentRefundList.size());
            List<Long> classIds = BeanUtils.toList(studentRefundList, "classId");
            List<Long> studentIds = BeanUtils.toList(studentRefundList, "studentId");
            List<MergeStudentRegDetail> mergeStudentRegDetailList=mergeStudentRegDetailService.findByStudentIdsClassIds(studentIds,classIds);
            List<String> bonusClassIdList = BeanUtils.toList(mergeStudentRegDetailList, "bonusClassIds");
            bonusClassIdList=bonusClassIdList.stream().filter(item->StringUtils.isNotEmpty(item)).collect(Collectors.toList());
            List<Long> bonusClassIds = Utils.toArrayLongIds(StringUtils.join(bonusClassIdList, ","));
            List<CourseClass> courseClassList=courseClassService.findByIds(bonusClassIds,"classId","teacherId");
            Map<Long, CourseClass> classMap = BeanUtils.toMap(courseClassList, "classId");

            Map<String,MergeStudentRegDetail> map=new HashMap<>();
            for (MergeStudentRegDetail mergeStudentRegDetail:mergeStudentRegDetailList){
                String key=mergeStudentRegDetail.getClassId()+"_"+mergeStudentRegDetail.getStudentId();
                map.put(key,mergeStudentRegDetail);
            }
            for (StudentRefund studentRefund:studentRefundList){
                String key=studentRefund.getClassId()+"_"+studentRefund.getStudentId();
                MergeStudentRegDetail mergeStudentRegDetail=map.get(key);
                if (mergeStudentRegDetail==null ){
                    continue;
                }
                RefundBonusPerson refundBonusPerson=new RefundBonusPerson();

                if (StringUtils.isNotEmpty(mergeStudentRegDetail.getBonusPeriodPersonIds())){
                    String[] bonusPeriodPersonIds=mergeStudentRegDetail.getBonusPeriodPersonIds().split(",");
                    List<String> seniorIds=new ArrayList<>(bonusPeriodPersonIds.length);
                    for (String periodPersonId:bonusPeriodPersonIds){
                        seniorIds.add(periodPersonId.split("_")[1]);
                    }
                    refundBonusPerson.setBonusSeniorIds(StringUtils.join(seniorIds,","));
                }
                if (StringUtils.isNotEmpty(mergeStudentRegDetail.getBonusClassIds())){
                    List<Long> haveBonusClassIds=Utils.toArrayLongIds(mergeStudentRegDetail.getBonusClassIds());
                    List<Long> bonusTeacherIds=new ArrayList<>();
                    for (Long classId:haveBonusClassIds){
                        CourseClass courseClass=classMap.get(classId);
                        if (courseClass!=null && courseClass.getTeacherId()!=null){
                            bonusTeacherIds.add(courseClass.getTeacherId());
                        }
                    }
                    refundBonusPerson.setBonusTeacherIds(StringUtils.join(bonusTeacherIds,","));
                }
                refundBonusPerson.setStudentRefundId(studentRefund.getStudentRefundId());
                refundBonusPerson.setCreateTime(TimeUtils.now());
                refundBonusPerson.setUpdateTime(TimeUtils.now());
                refundBonusPerson.setOperatorId(principal.id);
                insertList.add(refundBonusPerson);
            }
            if (insertList.size()>0){
                this.insertBatch(insertList);
            }


        }


    }

    @Override
    public void deleteByStudentRefundIds(List<Long> studentRefundIds) {
        if (Utils.isEmpty(studentRefundIds)){
            return;
        }
        this.deleteBatchIds(studentRefundIds);
    }
}